package ui.swing.table;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 功能：Table的选择模型（ListSelectionModel）的应用，注意TableModel和TableColumnModel
 *       的实现类都有ListSelectionModel对象，来配置选择的方式、触发相应的事件。
 *       TableModel的ListSelectionModel负责配置行数据的选择，而
 *       TableColumnModel的ListSelectionModel负责配置列的选择
 * @author 林可
 * @version 1.0 2009-11-10
 */
public class Table_SelectionListener extends JFrame implements ListSelectionListener, ActionListener{

	private static final long serialVersionUID = -6969519702716158949L;

	JTable table = null;
	JLabel lbl_status = null;
	
	public Table_SelectionListener() {
		super("选择模型的简单操作");
		setBounds(200, 200, 600, 400);
		
		String[] name = {"字段1","字段2","字段3","字段4","字段5"};
		String[][] data = new String[][]{
				{"11","12","13","14","15"},
				{"21","22","23","24","25"},
				{"31","32","33","34","35"},
				{"41","42","43","44","45"},
				{"51","52","53","54","55"}
		};
		
		table = new JTable(data, name);
		// 使得表格的选取是以cell为单位,而不是以列为单位.若你没有写此行,则在选取表格数
        // 据时以整列为单位.
		table.setCellSelectionEnabled(true);
		// table.setSelectionModel(new MyListSelectionModel());
		table.getSelectionModel().addListSelectionListener(this);
		table.getColumnModel().getSelectionModel().addListSelectionListener(this);

		JPanel pnl_tblContainer = new JPanel(new BorderLayout());
		pnl_tblContainer.add(table, BorderLayout.CENTER);
		pnl_tblContainer.add(table.getTableHeader(), BorderLayout.NORTH);
		
		JPanel pnl_btnContainer = new JPanel();
		JButton b=new JButton("单一选择");
		b.addActionListener(this);
		pnl_btnContainer.add(b);

		b = new JButton("连续区间选择");
		b.addActionListener(this);
		pnl_btnContainer.add(b);

		b = new JButton("多重选择");
		b.addActionListener(this);
		pnl_btnContainer.add(b);
		
		lbl_status = new JLabel("你选取了：");
		getContentPane().add(pnl_btnContainer, BorderLayout.NORTH);
		getContentPane().add(pnl_tblContainer, BorderLayout.CENTER);
		getContentPane().add(lbl_status, BorderLayout.SOUTH);

		/*MouseInputListener lis_mouseInput = new MouseInputAdapter() {
			int row = -1;
			int col = -1;
			@Override
			public void mousePressed(MouseEvent e) {
				boolean flg_changed = false;
				if(row!=-1 && table.getSelectedRow() == row && table.getSelectedColumn() != col)
				{
					flg_changed = true;
				}

				row = table.getSelectedRow();
				col = table.getSelectedColumn();
				if(flg_changed)
				{
					syncStatus();
				}
			}
			
			@Override
			public void mouseDragged(MouseEvent e) {
				mousePressed(e);
			}
		};
		
		table.addMouseListener(null);
		table.addMouseMotionListener(null);*/
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	// Table对应的ListSelectionModel注册的ListSelectionListener所监听的事件：
	public void valueChanged(ListSelectionEvent e) {
		syncStatus();
	}
	
	private void syncStatus() {
		int[] rowIndexes = table.getSelectedRows();
		int[] columnIndexes = table.getSelectedColumns();
		
		StringBuilder strbldr = new StringBuilder();
		for(int rowIndex : rowIndexes){
			for(int colIndex : columnIndexes) {
				strbldr.append(table.getValueAt(rowIndex, colIndex)).append(' ');
			}
		}
		String status = strbldr.toString();
		if(!status.equals("")) {
			status = status.substring(0, status.length() - 1);
		}
		lbl_status.setText("你选取了：" + status);
	}

	// 通过ListSelectionModel.setSelectionMode()设置选择模型
	// 注意看清楚哈是setSelectionMode,少了个l
	public void actionPerformed(ActionEvent e) {
		if("单一选择".equals(e.getActionCommand()))
		{
			table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			table.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		}
		if("连续区间选择".equals(e.getActionCommand()))
		{
			table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			table.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		}
		if("多重选择".equals(e.getActionCommand()))
		{
			table.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
			table.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		}		
	}
	
	/*static class MyListSelectionModel extends DefaultListSelectionModel {

		private static final long serialVersionUID = 5338592220618713831L;

		@Override
		public void fireValueChanged(boolean isAdjusting) {
			// TODO Auto-generated method stub
			super.fireValueChanged(isAdjusting);
		}
	}*/
	
	public static void main(String[] args) {
		new Table_SelectionListener();
	}
}
